package com.jh.tools.common.http;

import com.jh.tools.api.manager.RetrofitManager;
import com.jh.tools.bean.ImagesListEntity;
import com.jh.tools.bean.MusicsListEntity;
import com.jh.tools.bean.NeteastNewsDetail;
import com.jh.tools.bean.NeteastNewsSummary;
import com.jh.tools.bean.SinaPhotoDetail;
import com.jh.tools.bean.SinaPhotoList;
import com.jh.tools.bean.VideosListUserEntity;

import java.util.List;
import java.util.Map;

import rx.Observable;
import rx.functions.Func1;
import rx.functions.Func2;

/**
 * Created by niejiahuan on 16/6/24.
 * 请求管理
 */
public class RequestMange {
    private static RequestMange mRequestMange;
    public static RequestMange getInstance(){
        if (null==mRequestMange){
            mRequestMange=new RequestMange();
        }
        return mRequestMange;
    }

    /**
     * 获取新闻列表
     * @param startPage
     * @return
     */
    public Observable<Map<String, List<NeteastNewsSummary>>> getNewsListObservable(final Map<String,String> params, int startPage){
        Observable<Map<String, List<NeteastNewsSummary>>> mapObservable=RetrofitManager.builder(HttpUrlManage.UrlApi.WANGYI_URLS).getNewsListObservable(params.get("news_type"),params.get("news_id"),startPage);
        mapObservable.flatMap(
                new Func1<Map<String, List<NeteastNewsSummary>>, Observable<NeteastNewsSummary>>() {
                    @Override
                    public Observable<NeteastNewsSummary> call(Map<String, List<NeteastNewsSummary>> map) {
                        // 通过id取到list
                        return Observable.from(map.get(params.get("news_id")));
                    }
                })
                .toSortedList(new Func2<NeteastNewsSummary, NeteastNewsSummary, Integer>() {
                    @Override
                    public Integer call(NeteastNewsSummary neteastVideoSummary, NeteastNewsSummary neteastVideoSummary2) {
                        // 时间排序
                        return neteastVideoSummary.ptime.compareTo(neteastVideoSummary2.ptime);
                    }
                });
        return mapObservable;
    }

    /**
     * 获取新闻详细信息
     * @param postId
     * @return
     */
    public Observable<Map<String,NeteastNewsDetail>> getNewsDetailObservable(String postId){
        Observable<Map<String, NeteastNewsDetail>> mapObservable=RetrofitManager.builder(HttpUrlManage.UrlApi.WANGYI_URLS).getNewsDetailObservable(postId);
        return mapObservable;
    }

    /**
     * 新浪图片新闻列表
     * @param photoTypeId
     * @param page
     * @return
     */
    public Observable<SinaPhotoList> getSinaPhotoListObservable(String photoTypeId, int page){
        Observable<SinaPhotoList> mapObservable=RetrofitManager.builder(HttpUrlManage.UrlApi.SINA_PHOTO_HOST).getSinaPhotoListObservable(photoTypeId,page);
        return mapObservable;
    }

    /**
     * 图片详情
     * @param id
     * @return
     */
    public Observable<SinaPhotoDetail> getSinaPhotoDetailObservable(String id){
        Observable<SinaPhotoDetail> mapObservable=RetrofitManager.builder(HttpUrlManage.UrlApi.SINA_PHOTO_HOST).getSinaPhotoDetailObservable(id);
        return mapObservable;
    }

    /**
     * 获取百度图片
     * @param category
     * @param pageNum
     * @return
     */
    public  Observable<ImagesListEntity> getImagesListUrl(String category, String pageNum){
        Observable<ImagesListEntity> mapObservable=RetrofitManager.builder(HttpUrlManage.UrlApi.BAIDU_IMAGES_URLS).getImagesListUrl(category,pageNum);
        return mapObservable;
    }

    /**
     * 获取优酷视频列表
     * @param category
     * @param pageNum
     * @return
     */
    public Observable<VideosListUserEntity> getVideosListUrl(String category, String pageNum){
        Observable<VideosListUserEntity> mapObservable=RetrofitManager.builder(HttpUrlManage.UrlApi.YOUKU_VIDEOS_URLS).getVideosListUrl(category,pageNum);
        return mapObservable;
    }

    /**
     * 获取豆瓣音乐列表
     * @param channel
     * @return
     */
    public Observable<MusicsListEntity> getDoubanMusicsPlayListUrl(String channel){
        Observable<MusicsListEntity> mapObservable=RetrofitManager.builder(HttpUrlManage.UrlApi.DOUBAN_PLAY_LIST_URLS).getDoubanMusicsPlayListUrl(channel);
        return mapObservable;
    }
}
